<?php

class SimpleMessageWorkflow extends RunTimeWorkflow
{
  
  const IN_SUBJECT = 'subject';
  const IN_MESSAGE = 'message';
  
  /**
   * @var AbstractRecipientDeterminationSet
   */
  protected $recipients;

  /**
   * @param wfWorkflowInstance|string $param1
   * @param string $message
   * @param array|AbstractRecipientDeterminationSet $recipients
   * @param array $options
   */
  public function __construct ($param1, $message = '', $recipients = array(), $options = array())
  {
    if ($param1 instanceof wfWorkflowInstance)
    {
      parent::__construct($param1);
    }
    else
    {
      $container = new KeyValueWorkflowContainer();
      $container->setValue(self::IN_SUBJECT, $param1);
      $container->setValue(self::IN_MESSAGE, $message);
      
      if ($recipients instanceof AbstractRecipientDeterminationSet)
      {
        $this->recipients = $recipients;
      }
      elseif (is_array($recipients))
      {
        $this->recipients = new DynamicRecipientDeterminationSet();
        foreach ($recipients as $recipient)
        {
          $this->recipients->addRule(new UserIdRecipientDeterminationRule($recipient));
        }
      }
      
      parent::__construct($container, $options);
    }
  }

  protected function configure ()
  {
    
    $this->getDesignTime()
      ->startNode()
      ->followedByParallelSplitWithForeachable('distributer', $this->recipients)
        ->followedBy(SimpleMessageWorkflowNode::createDesignTimeNode('iterator_message'))
          ->mapIn(self::IN_SUBJECT, SimpleMessageWorkflowNode::IN_SUBJECT)
          ->mapIn(self::IN_MESSAGE, SimpleMessageWorkflowNode::IN_MESSAGE)
          ->addAction(DeleteWorkflowNodeAction::createDesignTimeAction('delete'))
          ->addAction(MarkUnreadWorkflowNodeAction::createDesignTimeAction('mark_unread'))
      ->endWithSynchronization()
      ->endNode();
  
  }
}